package com.service.source;


import com.entity.Source;
import com.facade.source.ISourceFacadeR;
import com.mapper.CustomMapper;
import com.mapper.SourceMapper;
import com.service.CustomServiceR;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SourceServiceR extends CustomServiceR<Source> implements ISourceFacadeR {

    @Resource
    private SourceMapper mapper;

    @Override
    protected CustomMapper<Source> initMapper() {
        return mapper;
    }

    @Override
    public List<Source> findByUserLink(Serializable userId, String... sortOrder) {
        if (null != userId) {
            Map<String, Object> params = new HashMap<>();
            params.put("userId", userId);
            StringBuilder sb0 = new StringBuilder();
            if (null != sortOrder) {
                for (int i = 0; i < sortOrder.length; i++) {
                    sb0.append(sortOrder[i]);
                    if (i != sortOrder.length - 1) {
                        sb0.append(", ");
                    }
                }
            }
            params.put("sortOrder", sb0.toString());
            return mapper.findByUserLink(params);
        }
        return Collections.EMPTY_LIST;
    }

    @Override
    public List<Source> findByUserAndSourceUrlsLink(Serializable userId, String... sourceUrls) {
        if (null != userId) {
            List<String> sourceUrlList = new ArrayList<>();
            if (null != sourceUrls) {
                for (String sourceUrl : sourceUrls) {
                    if (StringUtils.isNotBlank(sourceUrl)) {
                        sourceUrlList.add(sourceUrl.trim());
                    }
                }
            }
            if (sourceUrlList.size() == 0) {
                return findByUserLink(userId);
            }
            Map<String, Object> params = new HashMap<>();
            params.put("userId", userId);
            params.put("sourceUrls", sourceUrlList);
            return mapper.findByUserAndSourceUrlsLink(params);
        }
        return Collections.EMPTY_LIST;
    }

    @Override
    public List<Source> findByRoleLink(Serializable roleId, String... sortOrder) {
        if (null != roleId) {
            Map<String, Object> params = new HashMap<>();
            params.put("roleId", roleId);
            StringBuilder sb0 = new StringBuilder();
            if (null != sortOrder) {
                for (int i = 0; i < sortOrder.length; i++) {
                    sb0.append(sortOrder[i]);
                    if (i != sortOrder.length - 1) {
                        sb0.append(", ");
                    }
                }
            }
            params.put("sortOrder", sb0.toString());
            return mapper.findByRoleLink(params);
        }
        return Collections.EMPTY_LIST;
    }
}
